// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets;

import java.nio.ByteBuffer;
import zombie.characters.IsoZombie;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.network.NetworkVariables;

public class ZombiePacket implements INetworkPacket {
    private static final int PACKET_SIZE_BYTES = 55;
    public short id;
    public float x;
    public float y;
    public byte z;
    public int descriptorID;
    public NetworkVariables.PredictionTypes moveType;
    public short booleanVariables;
    public short target;
    public int timeSinceSeenFlesh;
    public int smParamTargetAngle;
    public short speedMod;
    public NetworkVariables.WalkType walkType;
    public float realX;
    public float realY;
    public byte realZ;
    public short realHealth;
    public NetworkVariables.ZombieState realState;
    public short reanimatedBodyID;
    public byte pfbType;
    public short pfbTarget;
    public float pfbTargetX;
    public float pfbTargetY;
    public byte pfbTargetZ;

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        this.id = b.getShort();
        this.x = b.getFloat();
        this.y = b.getFloat();
        this.z = b.get();
        this.descriptorID = b.getInt();
        this.moveType = NetworkVariables.PredictionTypes.fromByte(b.get());
        this.booleanVariables = b.getShort();
        this.target = b.getShort();
        this.timeSinceSeenFlesh = b.getInt();
        this.smParamTargetAngle = b.getInt();
        this.speedMod = b.getShort();
        this.walkType = NetworkVariables.WalkType.fromByte(b.get());
        this.realX = b.getFloat();
        this.realY = b.getFloat();
        this.realZ = b.get();
        this.realHealth = b.getShort();
        this.realState = NetworkVariables.ZombieState.fromByte(b.get());
        this.reanimatedBodyID = b.getShort();
        this.pfbType = b.get();
        if (this.pfbType == 1) {
            this.pfbTarget = b.getShort();
        } else if (this.pfbType > 1) {
            this.pfbTargetX = b.getFloat();
            this.pfbTargetY = b.getFloat();
            this.pfbTargetZ = b.get();
        }
    }

    public void write(ByteBuffer bb) {
        bb.putShort(this.id);
        bb.putFloat(this.x);
        bb.putFloat(this.y);
        bb.put(this.z);
        bb.putInt(this.descriptorID);
        bb.put((byte)this.moveType.ordinal());
        bb.putShort(this.booleanVariables);
        bb.putShort(this.target);
        bb.putInt(this.timeSinceSeenFlesh);
        bb.putInt(this.smParamTargetAngle);
        bb.putShort(this.speedMod);
        bb.put((byte)this.walkType.ordinal());
        bb.putFloat(this.realX);
        bb.putFloat(this.realY);
        bb.put(this.realZ);
        bb.putShort(this.realHealth);
        bb.put((byte)this.realState.ordinal());
        bb.putShort(this.reanimatedBodyID);
        bb.put(this.pfbType);
        if (this.pfbType == 1) {
            bb.putShort(this.pfbTarget);
        } else if (this.pfbType > 1) {
            bb.putFloat(this.pfbTargetX);
            bb.putFloat(this.pfbTargetY);
            bb.put(this.pfbTargetZ);
        }
    }

    @Override
    public void write(ByteBufferWriter b) {
        this.write(b.bb);
    }

    @Override
    public int getPacketSizeBytes() {
        return 55;
    }

    public void copy(ZombiePacket packet) {
        this.id = packet.id;
        this.x = packet.x;
        this.y = packet.y;
        this.z = packet.z;
        this.descriptorID = packet.descriptorID;
        this.moveType = packet.moveType;
        this.booleanVariables = packet.booleanVariables;
        this.target = packet.target;
        this.timeSinceSeenFlesh = packet.timeSinceSeenFlesh;
        this.smParamTargetAngle = packet.smParamTargetAngle;
        this.speedMod = packet.speedMod;
        this.walkType = packet.walkType;
        this.realX = packet.realX;
        this.realY = packet.realY;
        this.realZ = packet.realZ;
        this.realHealth = packet.realHealth;
        this.reanimatedBodyID = packet.reanimatedBodyID;
        this.realState = packet.realState;
        this.pfbType = packet.pfbType;
        this.pfbTarget = packet.pfbTarget;
        this.pfbTargetX = packet.pfbTargetX;
        this.pfbTargetY = packet.pfbTargetY;
        this.pfbTargetZ = packet.pfbTargetZ;
    }

    public void set(IsoZombie chr) {
        this.id = chr.OnlineID;
        this.descriptorID = chr.getPersistentOutfitID();
        chr.networkAI.set(this);
        chr.networkAI.mindSync.set(this);
        chr.thumpSent = true;
    }
}
